<!-- 调用一个点和线输入的PowerCopy -->
<template>
    <div>
        <el-card>
            <template #header>
                <span class="title-span">API基础信息</span>
            </template>
            <el-form label-width="100">
                <el-form-item label="节点名称：">
                    <el-input v-model="props.showFlowData.data.label"></el-input>
                </el-form-item>
                <el-form-item label="描述：">
                    <el-input v-model="props.showFlowData.data.desc" type="textarea"></el-input>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card style="margin-top: 10px;">
            <template #header>
                <span class="title-span">API状态</span>
            </template>
            <el-form label-width="100">
                <el-form-item label="名称：">
                    <el-input v-model="popShowData.APIState.name" disabled></el-input>
                </el-form-item>
                <el-form-item label="类型：">
                    <el-input v-model="popShowData.APIState.type" disabled></el-input>
                </el-form-item>
            </el-form>
        </el-card>

        <el-card style="margin-top: 10px;">
            <template #header>
                <span class="title-span">API输入</span>
            </template>
            <el-form label-width="100">
                <el-form-item label="名称：">
                    <el-input v-model="popShowData.APIInput.dataOne.name" disabled />
                </el-form-item>
                <el-form-item label="类型：">
                    <el-input v-model="popShowData.APIInput.dataOne.type" disabled />
                </el-form-item>
                <el-form-item label="值：">
                    <el-select v-model="popShowData.APIInput.dataOne.selectValue">
                        <el-option v-for="(item, index) in three_data_selectList" :key="index" :label="item.data.label"
                            :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <el-divider />
            <el-form label-width="100">
                <el-form-item label="名称：">
                    <el-input v-model="popShowData.APIInput.dataTwo.name" disabled />
                </el-form-item>
                <el-form-item label="类型：">
                    <el-input v-model="popShowData.APIInput.dataTwo.type" disabled />
                </el-form-item>
                <el-form-item label="值：">
                    <el-select v-model="popShowData.APIInput.dataTwo.selectValue">
                        <el-option v-for="(item, index) in element_list" :key="index" :label="item.data.label"
                            :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>

            <el-divider />
            <el-form label-width="100">
                <el-form-item label="名称：">
                    <el-input v-model="popShowData.APIInput.dataThree.name" disabled />
                </el-form-item>
                <el-form-item label="类型：">
                    <el-input v-model="popShowData.APIInput.dataThree.type" disabled />
                </el-form-item>
                <el-form-item label="值：">
                    <el-select v-model="popShowData.APIInput.dataThree.selectValue">
                        <el-option v-for="(item, index) in normal_line_list" :key="index" :label="item.data.label"
                            :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>

            <el-divider />
            <el-form label-width="100">
                <el-form-item label="名称：">
                    <el-input v-model="popShowData.APIInput.dataFour.name" disabled />
                </el-form-item>
                <el-form-item label="类型：">
                    <el-input v-model="popShowData.APIInput.dataFour.type" disabled />
                </el-form-item>
                <el-form-item label="值：">
                    <el-select v-model="popShowData.APIInput.dataFour.selectValue">
                        <el-option v-for="(item, index) in template_list" :key="index" :label="item.templateName"
                            :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>

        </el-card>


        <el-card style="margin-top: 10px;">
            <template #header>
                <span class="title-span">API输出</span>
            </template>
            <div v-for="(item, index) in APIOutput" :key="index">
                <el-form label-width="100">
                    <el-form-item label="名称：">
                        <el-input v-model="item.name" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="类型：">
                        <el-input v-model="item.type" disabled></el-input>
                    </el-form-item>
                </el-form>
            </div>
        </el-card>

    </div>
</template>

<script setup>
import { ref, defineProps, onMounted, watch } from 'vue';
import flowNodesStore from '@/store/modules/flowNodes'
import { computed } from 'vue';
const { proxy } = getCurrentInstance();
const flowNodes = flowNodesStore()
const template_list = ref(flowNodes.getTemplateList())
const props = defineProps({
    // Define props here
    showFlowData: {
        type: Object,
    },
    drawer: {
        type: Boolean,
    },
    recapture: {
        type: Boolean
    },
    inputSelectNodeIds: {
        type: Array
    }
});

const popShowData = ref({
    APIBaseData: {
        name: '调用一个点和线输入的PowerCopy',
        desc: '基于3D数据，获取2D图纸中需要标注的调用一个点和线输入的PowerCopy',
    },
    APIState: {
        name: '成功',
        type: '布尔'
    },
    APIInput: {
        dataOne: {
            name: '3D数据',
            type: '3D文档',
            selectValue: '',
        },
        dataTwo: {
            name: '点',
            type: '枚举',
            selectValue: '',
        },
        dataThree: {
            name: '线',
            type: '枚举',
            selectValue: '',
        },
        dataFour: {
            name: '模板选择',
            type: '枚举',
            selectValue: '',
        },
    },

    APIOutput: []
})

const node_data = computed(() => {
    let obj = {
        positionFlag: props.showFlowData.data.positionFlag,
        methodName: props.showFlowData.data.methodName,
        input3DData: popShowData.value.APIInput.dataOne.selectValue,
        input3DType: '',

        point: popShowData.value.APIInput.dataTwo.selectValue,
        inputPointsType: '',

        line: popShowData.value.APIInput.dataThree.selectValue,

        modelFile: '',
        modelName: '',
    }
    if (popShowData.value.APIInput.dataOne.selectValue) {
        let find_data = three_data_selectList.value.find(item => item.id == popShowData.value.APIInput.dataOne.selectValue);
        if (find_data) {
            obj.input3DType = find_data.data.type;
        }
    }
    if (popShowData.value.APIInput.dataTwo.selectValue) {
        let find_data = element_list.value.find(item => item.id == popShowData.value.APIInput.dataTwo.selectValue);
        if (find_data) {
            obj.inputPointsType = find_data.data.type;
        }
    }

    if (popShowData.value.APIInput.dataFour.selectValue) {
        let find_data = template_list.value.find(item => item.id == popShowData.value.APIInput.dataFour.selectValue);
        if (find_data) {
            obj.componetFile = find_data.templateFile;
            obj.modelName = find_data.templateName;
        }
    }
    return obj;
});

const three_data_selectList = computed(() => {
    let nodeList = flowNodes.getFlowNodes();
    const resultList = nodeList.filter(node => props.inputSelectNodeIds.includes(node.id)).filter(node => node.data.methodName === 'getCurrentData');

    // 遍历结果列表，根据 methodName 添加 type 属性
    resultList.forEach(node => {
        if (node.data.methodName === 'getCurrentData') {
            node.data.type = '3d';
        } else if (node.data.methodName === 'loop') {
            node.data.type = 'loop';
        }
    });

    return resultList;
});

const normal_line_list = computed(() => {
    let nodeList = flowNodes.getFlowNodes();
    const resultList = nodeList.filter(node => props.inputSelectNodeIds.includes(node.id)).filter(node => node.data.methodName === 'createNormalLineByBody');
    return resultList;
})

const element_list = computed(() => {
    let nodeList = flowNodes.getFlowNodes();
    const resultList = nodeList.filter(node => props.inputSelectNodeIds.includes(node.id)).filter(node => node.data.methodName == 'getAllPointFromGeometrialSet' || node.data.methodName === 'loop')

    // 遍历结果列表，根据 methodName 添加 type 属性
    resultList.forEach(node => {
        if (node.data.methodName === 'getAllPointFromGeometrialSet') {
            node.data.type = 'Points';
        } else if (node.data.methodName === 'loop') {
            node.data.type = 'loopNext';
        }
    });

    return resultList;
});

const APIOutput = computed(() => {
    const allCheckLists = [{
        name: '调用一个点和线输入的PowerCopy',
        type: '元素'
    }];
    return allCheckLists;
});

// watch(() => props.drawer, (val) => {
//     if (!val) {
//         setData();// 更新数据
//     }
// }, { deep: true }, { immediate: true })

watch(() => popShowData.value, (val) => {
    if (val) {
        setData();// 更新数据
    }
}, { deep: true }, { immediate: true })

watch(() => props.recapture, (val) => {
    reset();
    getList();
}, { deep: true }, { immediate: true })

watch(() => APIOutput.value, (val) => {
    popShowData.value.APIOutput = val
})

onMounted(() => {
    getList();
    setData();
})
const getList = () => {
    if (props.showFlowData.id) {
        if (flowNodes.getFlowNodesById(props.showFlowData.id)) {
            let nodeData = flowNodes.getFlowNodesById(props.showFlowData.id);
            if (nodeData.data.popShowData) {
                popShowData.value = JSON.parse(JSON.stringify(nodeData.data.popShowData))
            }
        }
    }
}
const setData = () => {
    let nodeData = flowNodes.getFlowNodesById(props.showFlowData.id);
    // 如果找到了该节点，则更新数据
    if (nodeData) {
        nodeData.data.popShowData = popShowData.value
        nodeData.data.data = node_data.value
        flowNodes.setFlowNodesById(props.showFlowData.id, nodeData)
    }
}
const reset = () => {
    popShowData.value = {
        APIBaseData: {
            name: '调用一个点和线输入的PowerCopy',
            desc: '基于3D数据，获取2D图纸中需要标注的点元素',
        },
        APIState: {
            name: '成功',
            type: '布尔'
        },
        APIInput: {
            dataOne: {
                name: '3D数据',
                type: '3D文档',
                selectValue: '',
            },
            dataTwo: {
                name: '点',
                type: '文本',
                selectValue: '',
            },
            dataThree: {
                name: '几何体',
                type: '文本',
                selectValue: '',
            },
            dataFour: {
                name: '创建位置',
                type: '文本',
                selectValue: '',
            },
        },
        APIOutput: []
    }
}
</script>

<style scoped>
.checkbox-item-sty {
    display: block;
}

::v-deep .el-checkbox__label {
    font-size: 16px;
    /* 你可以根据需要调整字体大小 */
}
</style>